/**
 * Created by wenchuan.li on 2018/6/14 10:56.
 */
define(function () {

    var self,
        app = win.app,
        pageParameters,
        addressInfo = {
            GWAK001: '',// 客户编号
            GWAKL004: '',//客户名称
            GWAK025: '',// 区域县
            GWAK028: '',
            GWAK030: '',//电话
        },
        subData = {
            GWBN002: '',//拜访日期
            GWBN005: '1',//维系方式
            GWBN006: '',//拜访人员姓名
            GWBN007: '',//拜访人员职位
            GWBN008: '',//拜访人员角色
            GWBN009: '',//拜访地址
            GWBN010: '',//拜访目的
            GWBN011: '',//拜访内容
            GWBN012: '',//商机评定审核说明（主管回复）
            GWBN014: '',//备注
            GWAK001: ''//客户
        },
        G_BFFL = []//拜访内容分类
    ;

    app.newModule({

        init: function () {
            self = this;

            self._super({
                _readyBefore: function () {
                    pageParameters = self._pageParameters;
                    app.extend2({target:subData, res:pageParameters.item});
                    app.extendUpLow({target:addressInfo, res:pageParameters.item,extend:false});
                }
            }, win);
        },
        getBffl: function () {//获取拜访记录内容分类
            app.ajax({
                xurl: "api/wx/gwat161/getGwbfListByzz",
                cb: function (result) {
                    if (result.ok) {
                        G_BFFL.pushArray(result.data.data.addMap({msg:''}), true);
                        if (subData.GWBN011.indexOf('{') != -1) {
                            var _GWBN011 = JSON.parse(subData.GWBN011);
                            G_BFFL.forEach(function (v) {
                                v.msg = _GWBN011[v.GWBFL002];
                            });
                        }
                    }
                }
            });
        },
        _ready: function () {
            new Vue({
                el: app.getByid(doc, 'gwat161Info'),
                data: {
                    sub: subData,
                    address: addressInfo,
                    bffls: G_BFFL
                },
                methods: {
                    showCustomers: function () {//客户
                        app.openPage({
                            src: 'hn-bfjltb1/customers.html',
                            args: {
                                cb: function (info) {
                                    app.extend(addressInfo, info);
                                    subData.GWAK001 = addressInfo.GWAK001;
                                    subData.GWBN006 = '';
                                }
                            }
                        });
                    },
                    showBfrs: function () { // 拜访人
                        if (addressInfo.GWAK001 == '') {
                            app.toast('请选择客户');
                            return;
                        }
                        app.openPage({
                            src: 'hn-bfjltb/bfr.html',
                            args: {
                                GWBIID: addressInfo.GWAK001,
                                GWBINAME:'all',
                                cb: function (bfr) {
                                    subData.GWBN006 = bfr.GWBI002;//拜访人员姓名
                                    //subData.GWBN007 = bfr.gwbi005;//拜访人员职位
                                    //subData.GWBN008 = bfr.gwbi001;//拜访人员角色
                                }
                            }
                        });
                    },
                    submitData: function () {
                        var _subData = app.cloneJSON(subData);
                        var _GWBN011 = {};
                        G_BFFL.forEach(function (v) {
                            _GWBN011[v.GWBFL002] = v.msg;
                        });
                        _subData.GWBN011 = JSON.stringify(_GWBN011);

                        app.ajax({
                            xurl: "api/wx/bfjltb1/bfjltb1Edit",
                            xdata: {data: _subData},
                            loading: true,
                            toJsonStr: true,
                            rules: {
                                'data.GWAK001': {
                                    nul: [false, '请选择客户']
                                },
                                'data.GWBN002': {
                                    nul: [false, '请填写拜访日期']
                                },
                                'data.GWBN006': {
                                    nul: [false, '请选择拜访人']
                                },
                                'data.GWBN010': {
                                    nul: [false, '请输入拜访目的']
                                }
                            },
                            confirm: {
                                msg: '是否保存资料？'
                            },
                            cb: function (result) {
                                if (result.ok) {
                                    app.alert({
                                        msg: '数据提交成功',
                                        ok: function () {
                                            pageParameters.cb();
                                            app.back(1);
                                        }
                                    });
                                }
                            }
                        });
                    }
                }
            });
        },
        _lazy: function () {
            this._super();

            //获取拜访记录内容分类
            self.getBffl();

            subData.GWBN002 = (new Date()).Format('yyyy-MM-dd');

            mui('.mui-scroll-wrapper').scroll({
                deceleration: 0.0005
            });
        }
    });

});